package com.iwhalecloud.bss.kite.cucc.order.loader.impl;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.cucc.order.loader.IOrderInfoLoader;
import com.iwhalecloud.bss.kite.cucc.order.loader.InstLoaderFactory;
import com.iwhalecloud.bss.kite.dataservice.entity.OrderAttr;
import com.iwhalecloud.bss.kite.dataservice.entity.OrderItem;
import com.iwhalecloud.bss.kite.dataservice.entity.order.OrderItemChange;
import com.iwhalecloud.bss.kite.dataservice.mapper.order.OrderAttrMapper;
import com.iwhalecloud.bss.kite.dataservice.mapper.order.OrderItemChangeMapper;
import com.iwhalecloud.bss.kite.dataservice.mapper.order.OrderItemMapper;
import com.iwhalecloud.bss.kite.order.client.dto.OrderContainer;
import com.iwhalecloud.bss.kite.order.client.dto.WithdrawOrderRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;

@Service
public class OrderInfoLoader implements IOrderInfoLoader {

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private OrderItemChangeMapper orderItemChangeMapper;

    @Autowired
    private OrderAttrMapper orderAttrMapper;



//    @Autowired
//    private OrderItemRelMapper orderItemRelMapper;

    @Override
    public void loadOldOrderInfos(WithdrawOrderRequest request, String oldOrderItemId, String ownerCustId) {
        this.loadOrderItem(request, oldOrderItemId, ownerCustId);
        // this.loadOrderItemAttr(request, oldOrderItemId, ownerCustId);
        // this.loadOrderItemRel(request, oldOrderItemId, ownerCustId);
        this.loadChanges(request, oldOrderItemId, ownerCustId);
//        this.loadParty(request, oldOrderItemId, ownerCustId);
//        this.loadDeposit(request, oldOrderItemId, ownerCustId);
        this.loadOrderAttrs(request);
    }

    private void loadOrderItem(WithdrawOrderRequest request, String orderItemId, String ownerCustId) {
        Example example = new Example(OrderItem.class);
        example.createCriteria().andEqualTo("orderItemId", orderItemId).andEqualTo("belongCustId", ownerCustId);
        OrderItem orderItem = orderItemMapper.selectOneByExample(example);
        OrderContainer.ofRequest(request).addOrderItem(orderItem);
    }

    // private void loadOrderItemAttr(WithdrawOrderRequest request, String oldOrderItemId, String ownerCustId) {
    // //TODO
    // }

    private void loadOrderAttrs(WithdrawOrderRequest request) {
        if (request.getOrderContainer().getOrderAttrMap() != null
                && request.getOrderContainer().getOrderAttrMap().containsKey(request.getOldCustOrderId())) {
            return;
        }
        Example example = new Example(OrderAttr.class);
        example.createCriteria().andEqualTo("custOrderId", request.getOldCustOrderId()).andEqualTo("custId", request.getCustId());
        List<OrderAttr> orderAttrs = orderAttrMapper.selectByExample(example);
        if (!KiteListUtils.isEmpty(orderAttrs)) {
            if (request.getOrderContainer().getOrderAttrMap() == null) {
                request.getOrderContainer().setOrderAttrMap(new HashMap<>());
            }
            request.getOrderContainer().getOrderAttrMap().put(request.getOldCustOrderId(), orderAttrs);
        }
    }

    private void loadChanges(WithdrawOrderRequest request, String oldOrderItemId, String ownerCustId) {
        List<OrderItemChange> changes = this.orderItemChangeMapper.listOrderItemChanges(oldOrderItemId, ownerCustId);
        if (!KiteListUtils.isEmpty(changes)) {
            if (request.getOrderContainer().getOrderItemChangeMap() == null) {
                request.getOrderContainer().setOrderItemChangeMap(new HashMap<String, List<OrderItemChange>>());
            }
            request.getOrderContainer().getOrderItemChangeMap().put(oldOrderItemId, changes);

            for (OrderItemChange change : changes) {
                String tableName = change.getTableName();
                InstLoaderFactory.getInstLoader(tableName).loadInst(request, tableName, change.getInstId(), ownerCustId);
            }
        }
    }

//    private void loadParty(WithdrawOrderRequest request, String oldOrderItemId, String ownerCustId) {
//        List<OrderHandler> orderHandlers = orderHandlerMapper.listOrderHandlers(request.getOldCustOrderId(), oldOrderItemId, ownerCustId);
//        if (!KiteListUtils.isEmpty(orderHandlers)) {
//            if (request.getOrderContainer().getOrderHandlerMap() == null) {
//                request.getOrderContainer().setOrderHandlerMap(new HashMap<String, List<OrderHandler>>());
//            }
//            request.getOrderContainer().getOrderHandlerMap().put(request.getOldCustOrderId(), orderHandlers);
//        }
//    }
//
//    private void loadDeposit(WithdrawOrderRequest request, String oldOrderItemId, String ownerCustId) {
//        Example example = new Example(OrderDeposit.class);
//        example.createCriteria().andEqualTo("orderItemId", oldOrderItemId).andEqualTo("custId", ownerCustId);
//        List<OrderDeposit> orderDeposits = this.orderDepositMapper.selectByExample(example);
//        if (!KiteListUtils.isEmpty(orderDeposits)) {
//            if (request.getOrderContainer().getOrderDepositMap() == null) {
//                request.getOrderContainer().setOrderDepositMap(new HashMap<String, List<OrderDeposit>>());
//            }
//            request.getOrderContainer().getOrderDepositMap().put(oldOrderItemId, orderDeposits);
//        }
//    }

    // private void loadOrderItemRel(WithdrawOrderRequest request, String oldOrderItemId, String ownerCustId) {
    // /*Example example = new Example(OrderItemRel.class);
    // example.createCriteria().andEqualTo("orderItemId", oldOrderItemId).andEqualTo("custId", ownerCustId);
    // List<OrderItemRel> orderItemRels = this.orderItemRelMapper.selectByExample(example);
    // if (!KiteListUtils.isEmpty(orderItemRels)) {
    //
    // }*/
    // }

}
